﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace StreetSmartAdmin
{
    public partial class Ticket : System.Web.UI.Page
    {
        public Userinfo user;
        protected void Page_Load(object sender, EventArgs e)
        {
            ADODotNetCRUD crud = new ADODotNetCRUD();
            user = crud.GetUser(User.Identity.Name);
            ((Label)Header.FindControl("lblUser")).Text = (user.fullname);
            GridView1.PageSize = Convert.ToInt32(ddlpage.SelectedValue); 
        }

        protected void btnAddRecord_click(object sender, EventArgs e)
        {
            DetailsView1.ChangeMode(DetailsViewMode.Insert);
            
        }




        protected void btnAll_click(object sender, EventArgs e)
        {
            GridView1.DataSourceID = "SqlDataSource1";
        }

        public void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            DetailsView1.ChangeMode(DetailsViewMode.Edit);
            GridView1.DataBind();
        }


        public void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
        {
            GridView1.DataBind();
            DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
        }

        public void SelID_TYPEChanged(object sender, EventArgs e)
        {
            if (((DropDownList)DetailsView1.FindControl("SelID_TYPE")).SelectedValue.Equals("") || ((DropDownList)DetailsView1.FindControl("SelID_TYPE")).SelectedValue.Equals("NEW"))
            {
                ((MultiView)DetailsView1.FindControl("MultiView1")).ActiveViewIndex = 0;
            }

            else if (((DropDownList)DetailsView1.FindControl("SelID_TYPE")).SelectedValue.Equals("CI"))
            {
                ((MultiView)DetailsView1.FindControl("MultiView1")).ActiveViewIndex = 1;
            }

            else if (((DropDownList)DetailsView1.FindControl("SelID_TYPE")).SelectedValue.Equals("RESI"))
            {
                ((MultiView)DetailsView1.FindControl("MultiView1")).ActiveViewIndex = 2;
            }

        }

        protected void AddNewCommentOnClick(object sender, EventArgs e)
        {
            Button btncase = (Button)sender;
            string title = "CFID";
            string CFID = btncase.CommandArgument.ToString();

            string title2 = "OPERATOR";
            string value2 = user.fullname;

            insertRecord("tblTicket_Comment", title, CFID, title2, value2);

            DetailsView1.DataBind();
        }

        protected void insertRecord(string table, string ID, string value, string ID2, string value2)
        {
            string insertcommand = "insert into " + table + " (" + ID + "," + ID2 + ") values(" + value + ",'" + value2 + "');";
            string connectionString = null;
            System.Configuration.ConnectionStringSettings connString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"];
            if (null != connString)
                connectionString = connString.ConnectionString;

            System.Data.SqlClient.SqlConnection connection = null;
            connection = new System.Data.SqlClient.SqlConnection(connectionString);
            connection.Open();
            System.Data.SqlClient.SqlCommand command;
            command = connection.CreateCommand();
            command.CommandText = insertcommand;
            command.ExecuteNonQuery();
            connection.Close();
        }


        protected void btnUpload_Click(object sender, EventArgs e)
        {

            Button btn = (Button)sender;
            // Although I put only one http file control on the aspx page,
            // the following code can handle multiple file controls in a single aspx page.
            HttpFileCollection files = Request.Files;
            foreach (string fileTagName in files)
            {
                HttpPostedFile file = Request.Files[fileTagName];
                if (file.ContentLength > 0)
                {
                    // Due to the limit of the max for a int type, the largest file can be
                    // uploaded is 2147483647, which is very large anyway.
                    int size = file.ContentLength;
                    string name = file.FileName;
                    int position = name.LastIndexOf("\\");
                    name = name.Substring(position + 1);
                    string contentType = file.ContentType;
                    byte[] fileData = new byte[size];
                    file.InputStream.Read(fileData, 0, size);

                    //int CaseID = 2;
                    int CaseID = Int32.Parse(btn.CommandArgument.ToString());
                    //string CaseID = btn.CommandArgument.ToString();
                    //string CaseID = e.CommandArgument.ToString();

                    //FileUtilities.SaveFile(name, contentType, size, fileData);

                    using (SqlConnection connection = new SqlConnection())
                    {
                        string connectionString = null;
                        System.Configuration.ConnectionStringSettings connString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"];

                        connectionString = connString.ConnectionString;
                        System.Data.SqlClient.SqlConnection connectionsql = null;

                        connectionsql = new System.Data.SqlClient.SqlConnection(connectionString);
                        connectionsql.Open();



                        SqlCommand cmd = new SqlCommand();
                        cmd.Connection = connectionsql;
                        cmd.CommandTimeout = 0;
                        //INSERT INTO [uploadtest] ([NAME], [SIZE], [DATA]) VALUES (@NAME, @SIZE, @DATA)
                        string commandText = "INSERT INTO tblFeedback_Case_File (CASEID, NAME, SIZE, DATA)VALUES(@CASEID, @NAME, @SIZE, @DATA)";

                        cmd.CommandText = commandText;
                        cmd.CommandType = CommandType.Text;

                        cmd.Parameters.Add("@CASEID", SqlDbType.Int);
                        cmd.Parameters.Add("@NAME", SqlDbType.NVarChar, 100);
                        //cmd.Parameters.Add("@ContentType", SqlDbType.VarChar, 50);
                        cmd.Parameters.Add("@SIZE", SqlDbType.Int);
                        cmd.Parameters.Add("@DATA", SqlDbType.VarBinary);

                        cmd.Parameters["@CASEID"].Value = CaseID;
                        cmd.Parameters["@NAME"].Value = name;
                        //cmd.Parameters["@ContentType"].Value = contentType;
                        cmd.Parameters["@SIZE"].Value = size;
                        cmd.Parameters["@DATA"].Value = fileData;
                        cmd.ExecuteNonQuery();

                        connection.Close();
                    }
                }
                DetailsView1.DataBind();
                //GridView1.DataBind();
            }


        }




        protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
        {

            if (e.CommandName.Equals("Insert"))
            {

                ((TextBox)DetailsView1.FindControl("LOG_BY")).Text = (user.fullname);

            }
        }

        protected void Sel_Bound(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            if (ddl.SelectedIndex == 0)
            {
                ddl.SelectedIndex = 1;
            }

        }

        protected void export_Click(object sender, EventArgs e)
        {
            GridView1.AllowPaging = false;
            GridView1.DataBind();
            Response.Clear();
            Response.AppendHeader("Content-Disposition", "attachment;filename= " + Server.UrlEncode("Export.xls"));
            Response.ContentType = "application/ms-excel";
            this.EnableViewState = false;
            System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("EN", true);
            System.IO.StringWriter stringWrite = new System.IO.StringWriter(myCItrad);
            System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
            GridView1.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();
            GridView1.AllowPaging = true;
        }

        override public void VerifyRenderingInServerForm(System.Web.UI.Control control)
        { }

        protected void GridView1_RowDeleting(Object sender, GridViewDeleteEventArgs e)
        {
            TableCell cell = GridView1.Rows[e.RowIndex].Cells[2];
            if (cell.Text == (user.fullname))
            {

                //Message.Text = "You cannot delete customer Beaver.";
            }
            else
            {
                //Message.Text = "";
                e.Cancel = true;
            }
        }


  }
}